set more off
clear all
cap log close
cd "~/Dropbox/Farm Subsidies and Committees/analyses/final_tables"
log using final_tables.txt, text replace


use if election_year_desc >= 2009 using ../collapsed, clear
drop if any_neg == 1

* mark orgs
merge m:1 customer_code using ../org_ids, keep(1 3)
gen org = (_merge == 3)

* rescale dvs
gen voted_rescaled = voted*100
gen ran_rescaled = ran*100
gen winner_rescaled = winner*100

label var voted_rescaled "Voted $\times$ 100"
label var ran_rescaled "Ran $\times$ 100"
label var winner_rescaled "Won $\times$ 100"


* binary payments
gen pos_amt = amt_all > 0 
gen pos_amt_lag1 = amt_all_lag1 > 0
gen pos_amt_lead1 = amt_all_lead1 > 0

label var pos_amt "Received payment"
label var pos_amt_lag1 "Received payment (lagged)"
label var pos_amt_lead1 "Received payment (lead)"

drop if org == 1


// Table 3
eststo clear

eststo: areg voted_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled pos_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled pos_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_3.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


* log payments
gen log_amt = log(amt_all + 1)
gen log_amt_lag1 = log(amt_all_lag1 + 1)
gen log_amt_lead1 = log(amt_all_lead1 + 1)

label var log_amt "Log payment + 1"
label var log_amt_lag1 "Log payment + 1 (lagged)"
label var log_amt_lead1 "Log payment + 1 (lead)"


// Table 4
eststo clear

eststo: areg voted_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg voted_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg ran_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg ran_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

eststo: areg winner_rescaled log_amt, absorb(election_year_desc) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "No", replace

eststo: areg winner_rescaled log_amt i.election_year_desc, absorb(customer_code) cluster(customer_code)
estadd ysumm, mean
estadd loc yfe "Yes", replace
estadd loc ffe "Yes", replace

esttab using table_4.tex, se label drop(*election_year*) stats(ymean yfe ffe r2, label("Mean of outcome (\%)" "Year FE" "Farmer FE" "\$R^{2}\$")) nostar mgroups("Voted" "Ran" "Won", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles(none) collabels(none) mlabels(none) compress booktabs frag float width(\hsize) replace


log close
